Home |
| Latest | About | Random
# Week 8 Wednesday Dear Class, This week we continue our discussion with structs (and later classes). Last time we discussed **constructors (15.3)**. Today we look at **member functions (15.2)** and **defining member functions and constructors outside of the struct interface (15.7)** A simple completion based assignment **pHW5** is posted. Please attempt it for practice writing structs/classes and writing constructors and member functions. **Submit it by next Wednesday (11/25) 10PM.** Midterm 2 will probably be graded by next Monday. There will still be class next week Monday and Wednesday. But no class for next week Friday as that would be part of Thanksgiving holiday! -- B. . . . . . ## A brief review of constructor... (15.3) A brief review of `struct` and its constructor here: [[1-teaching/ucla-2026-fall-pic-10a/brief-overview-of-constructor|brief-overview-of-constructor]] . . . . . ## Member functions (15.2). ### A `struct` with two member variables and two constructors. Say we have a `struct Point` with member variables `x` and `y` that holds the coordinates as `double`s, and a constructor function that takes two `double`s, as well as a default constructor that sets a default `Point` to the origin `(0,0)`: ```cpp #include using namespace std; struct Point { double x, y; Point (double _x, double _y) : x(_x), y(_y) {} Point () : x(0), y(0) {} // default constructor }; int main() { Point a(3,4); // using constructor Point::Point(double, double) Point o; // default constructed return 0; } ``` . . . . . ### Printing a `Point` from outside. We can write functions that do things to our `struct`. For example for our `Point`, perhaps we like to print out its coordinate values. We can write a free function called `print` like below ```cpp void print(const Point& p) { cout << '(' << p.x << ", " << p.y << ')' << endl; } ``` - Can you explain why we choose this function signature `void print(const Point& p)` for `print`? That's great, but we can **make `Point` itself print it out!** By having something like `a.print()` where `a` is a `Point`! This is now a **member function** of the `struct Point`. . . . . . ### Let the `Point` print itself. To do this we look at our `struct` interface for `Point`, and add it inside, but compare it with how it is written when it was a free function: ```cpp #include using namespace std; struct Point { double x, y; Point (double _x, double _y) : x(_x), y(_y) {} Point () : x(0), y(0) {} // default constructor // Member function void print() const { cout << '(' << x << ", " << y << ')' << endl; } }; // A free function called print on a Point void print(const Point& p) { cout << '(' << p.x << ", " << p.y << ')' << endl; } int main() { Point a(3,4); // using constructor Point::Point(double, double) Point o; // default constructed print(a); a.print(); return 0; } ``` . . . . . ### Some observations and questions... - Note that we can access the member variables `x` and `y` directly. But how does it know what `x` and `y` are inside the function scope???????? Where is the parameter that refers to the object itself?? - The answer is the implicit parameter `self` again! - And what's up with the `const`? - It marks the implicit parameter `self` as a "reference" to `const`! - The full name of this member function is `Point::print` . . . . . ### What happens when `a.print()` is called. So let us explain what happens when you call `a.print()`: - A function scope for `Point::print` is created. - An **implicit parameter** `self` is created, and it "references" the **implicit argument** `a` itself. - Because this member function is marked `const`, the reference `self` is a **reference to `const`**! (So here `self` is akin to having the type `const Point&`) - Create any **explicit parameters**, in this case there is none. And pass in any **explicit arguments**, in this case there is also none. - Run the code in the function body `{ }`. - Destroy the scope. **Draw a diagram!** ![[1-teaching/ucla-2026-fall-pic-10a/---files/Week-8-Wednesday 2026-11-18 04.22.56.excalidraw.svg]] %%[[1-teaching/ucla-2026-fall-pic-10a/---files/Week-8-Wednesday 2026-11-18 04.22.56.excalidraw.md|🖋 Edit in Excalidraw]]%% . . . . . ### Practice writing member functions. Let us write to our `struct Point` interface these member functions: - `Point::update(double, double)`, that updates the `x` and `y` member variables. - `Point::distance` with two overloads: - `Point::distance()` that calculates the distance of the point to the origin. - `Point::distance(????? other_point)` that calculates the distance of the current point to `other_point`. Pay attention to what the function signatures would be, and mark functions `const` if appropriate. [[1-teaching/ucla-2026-fall-pic-10a/answers-to-point-struct|answers-to-point-struct]] . . . . ## Defining a member function outside of the `struct` interface. (15.7) We can also define a member function separately from its declaration, **so long as it is declared inside the struct already.** Then we can even define the member function **outside of the `struct` interface**, by using the member function's full name. For example, here is where we have `Point::print` defined inside the `struct` interface: ```cpp struct Point { double x, y; Point (double _x, double _y) : x(_x), y(_y) {} Point () : x(0), y(0) {} // default constructor // Member function void print() const { cout << '(' << x << ", " << y << ')' << endl; } }; ``` And here is how we can write it outside of the interface, **provided we have declared the member function in the interface already**: ```cpp struct Point { double x, y; // declaration of constructors Point (double _x, double _y); Point (); // Member function void print() const; // declaration, note the const mark. }; // definition of member function outside of the interface: void Point::print() const { cout << '(' << x << ", " << y << ')' << endl; } // We can even define constructors outside of the interface: Point::Point (double _x, double _y) : x(_x), y(_y) {} Point::Point () : x(0), y(0) {} ``` Remember. - You can "declare inside, and define outside", for both member functions and constructors! - When defining outside of the `struc` interface, use their **full name**, like `Point::print`, or `Point::Point`. - Using a member function that is declared before you define it will give a **build error.** - Keep the `const` mark in both places (inside and outside), if there it is `const`. We will speak more about `const` correctness next time (section 15.4)